package org.jeecg.modules.yc.web.controller;

/**
 * 统计分析
 */

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import lombok.extern.slf4j.Slf4j;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.system.query.QueryGenerator;
import org.jeecg.modules.yc.web.entity.Terminal;
import org.jeecg.modules.yc.web.entity.vo.GrowCard;
import org.jeecg.modules.yc.web.service.IAlarmReportService;
import org.jeecg.modules.yc.web.service.IPdDeviceService;
import org.jeecg.modules.yc.web.service.ITerminalService;
import org.jeecg.modules.yc.web.service.IYcAlarmReportService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;


import java.time.DayOfWeek;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.time.temporal.ChronoUnit;
import java.time.temporal.TemporalAdjusters;
import java.util.Map;

/**
 * @Description: 统计分析
 * @Author: jeecg-boot
 * @Date:   2023-12-25
 * @Version: V1.0
 */
@Api(tags="采集器")
@RestController
@RequestMapping("/yc/web/statisticAnalysis")
@Slf4j
public class StatisticAnalysisController {

    @Autowired
    private ITerminalService terminalService;
    @Autowired
    private IPdDeviceService pdDeviceService;
    @Autowired
    private IYcAlarmReportService ycAlarmReportService;


    @GetMapping(value = "/growCard")
    public Result<GrowCard> growCard() {
        GrowCard growCard = new GrowCard();
        growCard.setTerminalCount(terminalService.count());
        // 获取当前时间
        LocalDateTime now = LocalDateTime.now();
        // 计算 2 小时前的时间
        LocalDateTime twoHoursAgo = now.minus(2, ChronoUnit.HOURS);

        // 创建 QueryWrapper 对象
        QueryWrapper queryWrapper = new QueryWrapper<>();
        // 添加条件：createTime 大于等于 2 小时前的时间
        queryWrapper.ge("last_connect_time", twoHoursAgo);
        growCard.setLoseTerminalCount(terminalService.count(queryWrapper));
        growCard.setDeviceCount(pdDeviceService.count());
        queryWrapper.clear();
        queryWrapper.ge("communication_time",twoHoursAgo);
        growCard.setLoseDeviceCount(pdDeviceService.count(queryWrapper));
        growCard.setAllAlarmCount(ycAlarmReportService.count());
        // 获取今天的日期
        LocalDate today = LocalDate.now();
        // 获取今天 0 点的时间
        LocalDateTime startOfDay = today.atStartOfDay();
        // 获取今天 24 点的时间
        LocalDateTime endOfDay = today.plusDays(1).atStartOfDay();
        queryWrapper.clear();
        queryWrapper.between("create_time", startOfDay, endOfDay);
        growCard.setTodayAlarmCount(ycAlarmReportService.count(queryWrapper));
        return Result.OK(growCard);
    }



}
